//
//  ChoicenessViewController.m
//  HappyNews
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "ChoicenessViewController.h"

#import "ChoicenessTableViewCell.h"

#import "HNRequestManager.h"

#import "ChoicenessViewModel.h"

#import "DCPicScrollView.h"

#import "DCWebImageManager.h"

#import "ChoicenessViewTwoModel.h"

#import "HNChoicenessViewController.h"

#define ChoicenessURL @"http://vcsp.ifeng.com/vcsp/appData/recommendGroupByTeamid.do?channelId=127952-0&showType=single&useType=iPhone&positionId=&adapterNo=6.11.1&isNotModified=0"

@interface ChoicenessViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *ChoicenessTabelView;

@property (nonatomic, strong) NSMutableArray *headerArr;

@property (nonatomic, strong) NSMutableArray *titleArr;

@property (nonatomic, strong) NSMutableArray *imageArr;

@property (nonatomic, strong) NSMutableArray *sectionArr;

@property (nonatomic, strong) HNChoicenessViewController *ChoicenessHeader;



@end

@implementation ChoicenessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    _headerArr = [NSMutableArray array];
    [self addChoicenessTabelView];
    
    [self setUpChoicenessData];
    
    // 表头数据
   // 轮播图的2个数组(使用第三方)
    _titleArr = [NSMutableArray array];
    _imageArr = [NSMutableArray array];
    
    // cell的数组
    _sectionArr = [NSMutableArray array];
    
    
    
   
    
}
// 解析数据
- (void)setUpChoicenessData
{
    [HNRequestManager requestWithUrlString:ChoicenessURL parDic:nil method:GET finish:^(NSData *data) {
      
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
      
        NSArray *str = dic[@"header"];
       
        for (NSMutableDictionary *oneDic in str) {
          
            ChoicenessViewModel *model = [[ChoicenessViewModel alloc]init];
          
            [model setValuesForKeysWithDictionary:oneDic];
           
            [_titleArr addObject:model.title];
          
            [_imageArr addObject:model.image];
          
            [_headerArr addObject:model];
        }
        // 轮播图(第三方)
        DCPicScrollView *picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kScreenHeight / 3) WithImageUrls:_imageArr];
       
        picView.titleData = _titleArr;
        
        
        
        // 点击图片跳转
        [picView setImageViewDidTapAtIndex:^(NSInteger index) {
            
            if (_delegate && [_delegate respondsToSelector:@selector(addNavigationController:)]) {
               
                ChoicenessViewModel *model = _headerArr[index];
                
                
                [_delegate addNavigationController:model.guid];

            }
        }];
        
        
        
        picView.AutoScrollDelay = 3.0f;
        
        _ChoicenessTabelView.tableHeaderView = picView;
        
        [[DCWebImageManager shareManager]setDownloadImageRepeatCount:1];
        
        [[DCWebImageManager shareManager]setDownLoadImageError:^(NSError *error, NSString *url) {
            NSLog(@"%@",error);
        }];
        
        
        
        // cell
        NSArray *bodyListArr = dic[@"bodyList"];
        for (NSMutableDictionary *bodyListDic in bodyListArr) {
            ChoicenessViewModel *model = [[ChoicenessViewModel alloc]init];
            [model setValuesForKeysWithDictionary:bodyListDic];
            [_sectionArr addObject:model];
        }
        
        // 刷新
        [_ChoicenessTabelView reloadData];
    } error:^(NSError *error) {
        
    }];
    
}
// 添加tabelView
- (void)addChoicenessTabelView
{
    _ChoicenessTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 115) style:(UITableViewStyleGrouped)];
    
    _ChoicenessTabelView.delegate = self;
    
    _ChoicenessTabelView.dataSource = self;
    
    _ChoicenessTabelView.rowHeight = kScreenHeight / 6.5;
    
    [_ChoicenessTabelView registerClass:[ChoicenessTableViewCell class] forCellReuseIdentifier:@"ChoicenessTableViewCell"];
    
    [self.view addSubview:_ChoicenessTabelView];
    
}
// 分行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _sectionArr.count;
}
// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChoicenessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChoicenessTableViewCell" forIndexPath:indexPath];
    
    ChoicenessViewModel *model = _sectionArr[indexPath.row];
    
    cell.model = model;

    return cell;
}
// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_delegate && [_delegate respondsToSelector:@selector(addNavigationController:)]) {
        
        ChoicenessViewModel *model = _sectionArr[indexPath.row];
        
        
        [_delegate addNavigationController:model.guid];
        
    }
}



// 区头
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    ChoicenessViewModel *model = _sectionArr[section];
//   
//    //return  [self sectionHeader:model.groupTitle and:model.name];
//}
//// 返回区头View方法
//- (UIView *)sectionHeader:(NSString *)group and:(NSString *)name
//{
//    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
//   
//    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth / 3.2, 20)];
//  
//    titleLabel.text = group;
//   
//    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 140, 0, kScreenWidth / 2.8, 20)];
//   
//    nameLabel.textAlignment = NSTextAlignmentRight;
//    
//    nameLabel.text = name;
//    
//    nameLabel.font = [UIFont systemFontOfSize:12];
//    
//    nameLabel.textColor = [UIColor grayColor];
//    
//    [v addSubview:titleLabel];
//    
//    [v addSubview:nameLabel];
//    
//    return v;
//}
//// 区头高度
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//        return 0;
//    }
//    return 30;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
